package cn.itsource.hrm.service.impl;


import cn.itsource.doc.CourseDoc;
import cn.itsource.feign.client.ESFeignClient;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.domain.CourseDetail;
import cn.itsource.hrm.domain.CourseMarket;
import cn.itsource.hrm.dto.CourseAddDto;
import cn.itsource.hrm.mapper.CourseDetailMapper;
import cn.itsource.hrm.mapper.CourseMapper;
import cn.itsource.hrm.mapper.CourseMarketMapper;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.utils.AjaxResult;
import cn.itsource.hrm.utils.PageList;
import cn.itsource.hrm.utils.UserContext;
import cn.itsource.hrm.utils.vo.LoginInfo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
//静态导入
import java.util.Date;

import static cn.itsource.hrm.domain.Course.STATUS_OFFLINE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luo
 * @since 2020-04-21
 */
@Service
@Transactional //开启事务
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ESFeignClient esFeignClient;

    @Autowired
    private CourseDetailMapper courseDetailMapper  ;

    @Autowired
    private CourseMarketMapper courseMarketMapper ;

    @Override
    public void saveOrUpdate(CourseAddDto dto) {

        Course course = dto.getCourse();
        CourseMarket courseMarket = dto.getCourseMarket();
        CourseDetail courseDetail = dto.getCourseDetail();
        //首先课程名不为空才进行一下逻辑
        if(!StringUtils.hasLength(course.getName())){
            throw new RuntimeException("课程名字不能为空") ;
        }
        System.out.println(course.getGradeId());
        //第一步保存课程
        course.setStatus(STATUS_OFFLINE);
        //获取模拟的数据
        LoginInfo loginInfo = UserContext.getLoginInfo();
        //把摸你的登录数据获取出来 存入课程
        course.setUserId(loginInfo.getUserId());
        course.setUserName(loginInfo.getUserName());
        course.setTenantId(loginInfo.getTenantId());
        course.setTenantName(loginInfo.getTenantName());

        //直接插入
        baseMapper.insert(course);
        //第二部保存详情
        //id一对一 直接保存
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
        //第三部保存营销
        courseMarket.setValid(CourseMarket.STATE_VALID);
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket) ;
    }

    @Override
    public void offLineCourse(Long id) {
        System.out.println(id);
        //1.通过课程ID获取课程对象
        Course course = baseMapper.selectById(id);
        //2.判断对象不为空
        if(course == null){
            //报异常
            throw new RuntimeException("没有这个课程");
        }
        //3.判断该课程必须是已发布的  不是已发布的就会报错
        if (course.getStatus()!=Course.STATUS_ONLINE){
            throw new RuntimeException("该课程未发布不能下线");
        }
        //4.修改课程状态为下线
        course.setStatus(Course.STATUS_OFFLINE);
        //返回n行
        Integer integer = baseMapper.updateById(course);
        if(integer > 0){//修改成功 ，从es中移除
            AjaxResult ajaxResult = esFeignClient.offLineCourse(id);
            //回滚
            if(!ajaxResult.isSuccess()){
                throw new RuntimeException("下线失败");
            }
        }else {//修改失败
            throw new RuntimeException("课程状态修改失败");
        }
        //5把课程从ES中移除

    }
    //课程上线
    @Override
    public void onLineCourse(Long id) {
        //1.通过课程ID获取课程对象
        Course course = baseMapper.selectById(id);
        //2.判断对象不为空
        if(course == null){
            //报异常
            throw new RuntimeException("没有这个课程");
        }
        //3.判断该课程必须是未发布的  不是未发布的就会报错
        if (course.getStatus()!=Course.STATUS_OFFLINE){
            throw new RuntimeException("该课程不是未发布状态 无法上线");
        }
        //4.修改课程状态为下线
        course.setStatus(Course.STATUS_ONLINE);
        //返回n行
        Integer integer = baseMapper.updateById(course);
        if(integer > 0){//修改成功 ，从es添加
            //5把课程添加到es
            //获取课程营销和课程详情相关信息
            CourseMarket courseMarket = courseMarketMapper.selectById(id);
            CourseDetail courseDetail = courseDetailMapper.selectById(id);
            //通过工具类方便把三张表的数据设置到doc中
            CourseDoc courseDoc = new CourseDoc();
            BeanUtils.copyProperties(courseMarket,courseDoc);
            BeanUtils.copyProperties(courseDetail,courseDoc);
            //设置上线时间
            courseDoc.setOnlineTime(new Date());
            //转换一下类型
            courseDoc.setCharge(courseMarket.getCharge().intValue());
            BeanUtils.copyProperties(course,courseDoc);

            AjaxResult ajaxResult = esFeignClient.onLineCourse(courseDoc);
            if(!ajaxResult.isSuccess()){
                throw new RuntimeException("上线失败");
            }

        }else {//修改失败
            throw new RuntimeException("课程状态修改失败");
        }

    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        //逻辑交给es处理
        return esFeignClient.searchCourse(courseQuery);
    }
}


